import pytest
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
class Base():
    # 初始化用例
    def __init__(self,driver):
        self.driver = webdriver.Chrome()
    # 获取网址的方法
    def open_url(self,url):
        self.driver.get(url)
    #获取元素定位的方法
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver :self.driver.find_element(loc).is_displayed())
            return self.driver.find_element(loc)
        except Exception as e:
            print(f'元素未找到{e}')
    # 点击事件
    def text(self,loc):
        return self.driver.find_element(loc).text
    def click(self,loc):
        self.driver.find_element(loc).click()
    # 输入内容
    def send_keys(self,loc,info):
        self.driver.find_element(loc).send_keys(info)
    # 进入iframe
    def to_frame(self,loc):
        self.driver.switch_to.frame(self.driver.find_element(loc))
    # 退出iframe
    def back_frame(self):
        self.driver.switch_to.parent_frame()
    # 切换页面
    def put_window(self,loc):
        self.driver.window_handles.index(loc)